﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KenLib.BusinessManager;
using System.IO;

/// <summary>
/// 
/// </summary>
public partial class Org_frmO_EmpBasicInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((Org_MasterPage)this.Master).changeTitle("団体職員個人情報");
        ((Org_MasterPage)this.Master).HideLiArea("frmO_EmpBasicInfo");
        
        if (Session["役"] == null)
        {
            Response.Redirect("~/frmD_Login.aspx");
        }
        if (!IsPostBack)
        {
            if (Request.QueryString["id"] != null)
            {
                if (Session["or00110f_link"] != null)
                {
                    string link = Session["or00110f_link"].ToString();
                    Session["or00110f_link"] = null;
                    Response.Redirect("frmO_EmpBasicInfo.aspx?id=" + Request.QueryString["id"] + "#" + link);
                }
            }
            else
            {
                Response.Redirect("~/Org/frmO_Emplist.aspx");
            }
            SetDefaultGrv(grv_入退社情報);
            SetDefaultGrv(grv_雇用情報);
            SetDefaultGrv(grv_資格情報);
            SetDefaultGrv(grv_職種職務情報);
            SetDefaultGrv(grv_保有資格情報);
            SetDefaultGrv(grv_業務スキル情報);
            SetDefaultGrv(grv_職歴情報);
            SetDefaultGrv(grv_賞罰情報);
            SetDefaultGrv(grv_学歴情報);
        }
        LoadDetails();
    }

    /// <summary>
    /// Sets the default GRV.
    /// </summary>
    /// <param name="grv">The GRV.</param>
    private void SetDefaultGrv(GridView grv)
    {
        grv.PageSize = AppCode_Common.getPageSize();
        grv.PageIndex = 0;
    }
    /// <summary>
    /// 
    /// </summary>
    private string ListAtrr = "姓名,フリガナ姓名,性別,生年月日,年齢,国籍,出身地,携帯番号,会社メールアドレス,趣味,外国人登録番号,在留期限,パスポート番号" +
    ",発行日,有効期限,市民権";
    /// <summary>
    /// 
    /// </summary>
    private string List所属情報 = "部門名,役職,発令開始日,発令終了日,勤務地";
    /// <summary>
    /// Loads the details.
    /// </summary>
    private void LoadDetails()
    {
        if (Request.QueryString["id"] != null)
        {
            Int32 id;
            if (Int32.TryParse(Request.QueryString["id"], out id))
            {
                frmO_EmpBasicInfoManager cls_frmO_EmpBasicInfoManager = new frmO_EmpBasicInfoManager();
                DataSet dts = cls_frmO_EmpBasicInfoManager.select基本情報by社員ID(id);
                ViewState.Add("dts", dts);
                DataTable dtt_基本情報 = dts.Tables["基本情報"];
                if (dtt_基本情報 != null && dtt_基本情報.Rows.Count > 0)
                {

                    FillValueControl(dtt_基本情報.Rows[0], ListAtrr);
                    if (dtt_基本情報.Rows[0]["写真"].ToString() != "")
                    {
                        if (File.Exists(Server.MapPath("../Images/Org_Staff/" + dtt_基本情報.Rows[0]["写真"].ToString())))
                        {
                            img_写真.ImageUrl = "../Images/Org_Staff/" + dtt_基本情報.Rows[0]["写真"].ToString();
                        }
                    }
                    
                }
                else
                {
                    Response.Redirect("~/Org/frmO_Emplist.aspx");
                }
                //111
                FillDataTable(dts.Tables["入退社情報"], grv_入退社情報, "ID,採用区分,入社年月日,退社年月日,退職理由");
                //112
                FillDataTable(dts.Tables["雇用情報"], grv_雇用情報, "ID,雇用区分,発令開始日,実際開始日");
                //113
                FillDataTable(dts.Tables["資格情報"], grv_資格情報, "ID,資格名,発令開始日,発令終了日,実際開始日,実際終了日");
                //114
                if (dts.Tables["所属情報"].Rows.Count > 0)
                {
                    if (dts.Tables["所属情報"].Rows[0]["兼務"] != null)
                    {
                        if (dts.Tables["所属情報"].Rows[0]["兼務"].ToString() == "0")
                        {
                            lbl_兼務.Text = "主務";
                        }
                        else if (dts.Tables["所属情報"].Rows[0]["兼務"].ToString() == "1")
                        {
                            lbl_兼務.Text = "兼務";
                        }
                    }
                    FillValueControl(dts.Tables["所属情報"].Rows[0], List所属情報);
                }
                //115
                FillDataTable(dts.Tables["職種職務情報"], grv_職種職務情報, "ID,職種,職務,立場,実際開始日");
                //120
                FillDataTable(dts.Tables["保有資格情報"], grv_保有資格情報, "ID,資格免許,社内外区分,受検年月日,取得年月日");
                //121
                FillDataTable(dts.Tables["業務スキル情報"], grv_業務スキル情報, "ID,業務スキル,レベル,発行機関,発行日");
                //122
                FillDataTable(dts.Tables["賞罰情報"], grv_賞罰情報, "ID,賞罰名,賞罰年月日,賞罰特記事項");
                //124
                FillDataTable(dts.Tables["学歴情報"], grv_学歴情報, "ID,学歴区分,学校名,開始時期,終了時期");
                //125
                FillDataTable(dts.Tables["職歴情報"], grv_職歴情報, "ID,企業名,部門名,仕事内容,開始年月日,終了年月日");
            }
            else
            {
                Response.Redirect("~/Org/frmO_Emplist.aspx");
            }
        }
        else
        {
            Response.Redirect("~/Org/frmO_Emplist.aspx");
        }
    }

    /// <summary>
    /// Fills the data table.
    /// </summary>
    /// <param name="dtt">The DTT.</param>
    /// <param name="grv">The GRV.</param>
    /// <param name="header">The header.</param>
    private void FillDataTable(DataTable dtt, GridView grv, string header)
    {
        if (dtt.Rows.Count > 0)
        {
            grv.DataSource = dtt;
            grv.DataBind();
        }
        else
        {
            ShowGridViewNotClassEmpty(grv, header);
        }
    }

    /// <summary>
    /// Fills the value control.
    /// </summary>
    /// <param name="dtr">The DTR.</param>
    /// <param name="lst">The LST.</param>
    private void FillValueControl(DataRow dtr, String lst)
    {
        foreach (string str_ctr in lst.Split(','))
        {
            Label ctr = (Label)panelContent.FindControl("lbl_" + str_ctr);
            if (ctr != null)
            {
                switch (dtr[str_ctr].GetType().Name)
                {
                    case "DateTime" : 
                        ctr.Text = dtr[str_ctr].ToString() == "" ? "-" : ((DateTime)dtr[str_ctr]).ToString("yyyy年MM月dd日"); 
                        break;
                    default :
                        ctr.Text = dtr[str_ctr].ToString() == "" ? "-" : dtr[str_ctr].ToString();
                        break;
                }
                
            }
        }
    }

    /// <summary>
    /// Handles the Click event of the hpl_修正 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void hpl_修正_Click(object sender, EventArgs e)
    {
        Session["frmO_EmpBasicInfo"] = "true";
        string Id = Request.QueryString["id"] != null ? Request.QueryString["id"].ToString() : "";
        if (Id != "")
        {
            Session["or00110f_link"] = hdf_link.Value;
            switch (hdf_link.Value)
            {
                case "110":
                    Redirect("frmO_EmpBasicInfoInput.aspx", Id);
                    break;
                case "111":
                    Redirect("frmO_EmpEnterInput.aspx", Id);
                    break;
                case "112":
                    Redirect("frmO_EmpContractTypeInput.aspx", Id);
                    break;
                case "113":
                    Redirect("frmO_EmpDegreeByCompInput.aspx", Id);
                    break;
                case "114":
                    Redirect("frmO_EmpDivisionInput.aspx", Id);
                    break;
                case "115":
                    Redirect("frmO_EmpCareerInput.aspx", Id);
                    break;
                case "120":
                    Redirect("frmO_EmpDiplomaInput.aspx", Id);
                    break;
                case "121":
                    Redirect("frmO_EmpWorkSkillInput.aspx", Id);
                    break;
                case "122":
                    Redirect("frmO_EmpRewPunInput.aspx", Id);
                    break;
                case "124":
                    Redirect("frmO_EmpAcademicInput.aspx", Id);
                    break;
                case "125":
                    Redirect("frmO_EmpWorkedCompInput.aspx?", Id);
                    break;
            }
        }
    }

    /// <summary>
    /// Redirects the specified link.
    /// </summary>
    /// <param name="link">The link.</param>
    /// <param name="Id">The id.</param>
    private void Redirect(String link, String Id)
    {
        //if (grv.DataKeys[0].Value.ToString() == "")
        //{
            Response.Redirect(link + "?id=" + Id + "&id3=1");
        //}
        //else
        //{
        //    Response.Redirect(link + "?id=" + Id + "&id3=1");
        //}
    }

    /// <summary>
    /// Shows the grid view not class empty.
    /// </summary>
    /// <param name="grv">The GRV.</param>
    /// <param name="strTableHeader">The STR table header.</param>
    public void ShowGridViewNotClassEmpty(GridView grv, string strTableHeader)
    {
        string strEmpty = "データーがありません。";
        DataTable dt = new DataTable();
        string[] arrColumnNames = strTableHeader.Split(',');
        for (int i = 0; i < arrColumnNames.Length; i++)
        {
            dt.Columns.Add(arrColumnNames[i]);
        }
        grv.ShowFooter = false;
        DataRow r = dt.NewRow();
        dt.Rows.InsertAt(r, 0);
        grv.DataSource = dt;
        grv.DataBind();
        grv.Rows[0].Cells.Clear();
        TableCell cell = new TableCell();
        cell.ColumnSpan = grv.Columns.Count;
        cell.Text = strEmpty;
        grv.Rows[0].Cells.Add(cell);
    }

    /// <summary>
    /// Handles the PageIndexChanging event of the grv_入退社情報 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewPageEventArgs"/> instance containing the event data.</param>
    protected void grv_入退社情報_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (ViewState["dts"] != null)
        {
            grv_入退社情報.PageIndex = e.NewPageIndex;
            FillDataTable(((DataSet)ViewState["dts"]).Tables["入退社情報"], grv_入退社情報, "ID,採用区分,入社年月日,退社年月日,退職理由");
        }
    }

    /// <summary>
    /// Handles the RowCommand event of the grv_入退社情報 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCommandEventArgs"/> instance containing the event data.</param>
    protected void grv_入退社情報_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "link")
        {
            Session["frmO_EmpBasicInfo"] = "true";
            string Id = Request.QueryString["id"] != null ? Request.QueryString["id"].ToString() : "";
            string Id2 = grv_入退社情報.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
            if (Id != "")
            {
                Session["or00110f_link"] = "111";
                Response.Redirect("frmO_EmpEnterInput.aspx?id=" + Id + "&id2=" + Id2);
            }
        }
    }

    /// <summary>
    /// Handles the PageIndexChanging event of the grv_雇用情報 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewPageEventArgs"/> instance containing the event data.</param>
    protected void grv_雇用情報_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (ViewState["dts"] != null)
        {
            grv_雇用情報.PageIndex = e.NewPageIndex;
            FillDataTable(((DataSet)ViewState["dts"]).Tables["雇用情報"], grv_雇用情報, "ID,雇用区分,発令開始日,実際開始日");
        }
    }
    /// <summary>
    /// Handles the RowCommand event of the grv_雇用情報 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCommandEventArgs"/> instance containing the event data.</param>
    protected void grv_雇用情報_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "link")
        {
            string Id = Request.QueryString["id"] != null ? Request.QueryString["id"].ToString() : "";
            string Id2 = grv_雇用情報.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
            if (Id != "")
            {
                Session["or00110f_link"] = "112";
                Response.Redirect("frmO_EmpContractTypeInput.aspx?id=" + Id + "&id2=" + Id2);
            }
        }
    }

    /// <summary>
    /// Handles the PageIndexChanging event of the grv_資格情報 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewPageEventArgs"/> instance containing the event data.</param>
    protected void grv_資格情報_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (ViewState["dts"] != null)
        {
            grv_資格情報.PageIndex = e.NewPageIndex;
            FillDataTable(((DataSet)ViewState["dts"]).Tables["資格情報"], grv_資格情報, "ID,資格名,発令開始日,発令終了日,実際開始日,実際終了日");
        }
    }
    /// <summary>
    /// Handles the RowCommand event of the grv_資格情報 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCommandEventArgs"/> instance containing the event data.</param>
    protected void grv_資格情報_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "link")
        {
            string Id = Request.QueryString["id"] != null ? Request.QueryString["id"].ToString() : "";
            string Id2 = grv_資格情報.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
            if (Id != "")
            {
                Session["or00110f_link"] = "113";
                Response.Redirect("frmO_EmpDegreeByCompInput.aspx?id=" + Id + "&id2=" + Id2);
            }
        }
    }
    /// <summary>
    /// Handles the PageIndexChanging event of the grv_職種職務情報 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewPageEventArgs"/> instance containing the event data.</param>
    protected void grv_職種職務情報_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (ViewState["dts"] != null)
        {
            grv_職種職務情報.PageIndex = e.NewPageIndex;
            FillDataTable(((DataSet)ViewState["dts"]).Tables["職種職務情報"], grv_職種職務情報, "ID,職種,職務,立場,実際開始日");
        }
    }
    /// <summary>
    /// Handles the RowCommand event of the grv_職種職務情報 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCommandEventArgs"/> instance containing the event data.</param>
    protected void grv_職種職務情報_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "link")
        {
            string Id = Request.QueryString["id"] != null ? Request.QueryString["id"].ToString() : "";
            string Id2 = grv_職種職務情報.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
            if (Id != "")
            {
                Session["or00110f_link"] = "115";
                Response.Redirect("frmO_EmpCareerInput.aspx?id=" + Id + "&id2=" + Id2);
            }
        }
    }
    /// <summary>
    /// Handles the PageIndexChanging event of the grv_保有資格情報 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewPageEventArgs"/> instance containing the event data.</param>
    protected void grv_保有資格情報_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (ViewState["dts"] != null)
        {
            grv_保有資格情報.PageIndex = e.NewPageIndex;
            FillDataTable(((DataSet)ViewState["dts"]).Tables["保有資格情報"], grv_保有資格情報, "ID,資格免許,社内外区分,受検年月日,取得年月日");
        }
    }
    /// <summary>
    /// Handles the RowCommand event of the grv_保有資格情報 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCommandEventArgs"/> instance containing the event data.</param>
    protected void grv_保有資格情報_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "link")
        {
            string Id = Request.QueryString["id"] != null ? Request.QueryString["id"].ToString() : "";
            string Id2 = grv_保有資格情報.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
            if (Id != "")
            {
                Session["or00110f_link"] = "120";
                Response.Redirect("frmO_EmpDiplomaInput.aspx?id=" + Id + "&id2=" + Id2);
            }
        }
    }
    /// <summary>
    /// Handles the PageIndexChanging event of the grv_職歴情報 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewPageEventArgs"/> instance containing the event data.</param>
    protected void grv_職歴情報_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (ViewState["dts"] != null)
        {
            grv_職歴情報.PageIndex = e.NewPageIndex;
            FillDataTable(((DataSet)ViewState["dts"]).Tables["職歴情報"], grv_職歴情報, "ID,企業名,部門名,仕事内容,開始年月日,終了年月日");
        }
    }
    /// <summary>
    /// Handles the RowCommand event of the grv_職歴情報 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCommandEventArgs"/> instance containing the event data.</param>
    protected void grv_職歴情報_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "link")
        {
            string Id = Request.QueryString["id"] != null ? Request.QueryString["id"].ToString() : "";
            string Id2 = grv_職歴情報.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
            if (Id != "")
            {
                Session["or00110f_link"] = "125";
                Response.Redirect("frmO_EmpWorkedCompInput.aspx?id=" + Id + "&id2=" + Id2);
            }
        }
    }
    /// <summary>
    /// Handles the PageIndexChanging event of the grv_業務スキル情報 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewPageEventArgs"/> instance containing the event data.</param>
    protected void grv_業務スキル情報_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (ViewState["dts"] != null)
        {
            grv_業務スキル情報.PageIndex = e.NewPageIndex;
            FillDataTable(((DataSet)ViewState["dts"]).Tables["業務スキル情報"], grv_業務スキル情報, "ID,業務スキル,レベル,発行機関,発行日");
        }
    }
    /// <summary>
    /// Handles the RowCommand event of the grv_業務スキル情報 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCommandEventArgs"/> instance containing the event data.</param>
    protected void grv_業務スキル情報_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "link")
        {
            string Id = Request.QueryString["id"] != null ? Request.QueryString["id"].ToString() : "";
            string Id2 = grv_業務スキル情報.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
            if (Id != "")
            {
                Session["or00110f_link"] = "121";
                Response.Redirect("frmO_EmpWorkSkillInput.aspx?id=" + Id + "&id2=" + Id2);
            }
        }
    }
    /// <summary>
    /// Handles the PageIndexChanging event of the grv_賞罰情報 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewPageEventArgs"/> instance containing the event data.</param>
    protected void grv_賞罰情報_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (ViewState["dts"] != null)
        {
            grv_賞罰情報.PageIndex = e.NewPageIndex;
            FillDataTable(((DataSet)ViewState["dts"]).Tables["賞罰情報"], grv_賞罰情報, "ID,賞罰名,賞罰年月日,賞罰特記事項");
        }
    }
    /// <summary>
    /// Handles the RowCommand event of the grv_賞罰情報 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCommandEventArgs"/> instance containing the event data.</param>
    protected void grv_賞罰情報_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "link")
        {
            string Id = Request.QueryString["id"] != null ? Request.QueryString["id"].ToString() : "";
            string Id2 = grv_賞罰情報.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
            if (Id != "")
            {
                Session["or00110f_link"] = "122";
                Response.Redirect("frmO_EmpRewPunInput.aspx?id=" + Id + "&id2=" + Id2);
            }
        }
    }
    /// <summary>
    /// Handles the PageIndexChanging event of the grv_学歴情報 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewPageEventArgs"/> instance containing the event data.</param>
    protected void grv_学歴情報_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (ViewState["dts"] != null)
        {
            grv_学歴情報.PageIndex = e.NewPageIndex;
            FillDataTable(((DataSet)ViewState["dts"]).Tables["学歴情報"], grv_学歴情報, "ID,学歴区分,学校名,開始時期,終了時期");
        }
    }
    /// <summary>
    /// Handles the RowCommand event of the grv_学歴情報 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCommandEventArgs"/> instance containing the event data.</param>
    protected void grv_学歴情報_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "link")
        {
            string Id = Request.QueryString["id"] != null ? Request.QueryString["id"].ToString() : "";
            string Id2 = grv_学歴情報.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
            if (Id != "")
            {
                Session["or00110f_link"] = "124";
                Response.Redirect("frmO_EmpAcademicInput.aspx?id=" + Id + "&id2=" + Id2);
            }
        }
    }
}
